package cn.edu.sjtu.literature.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import cn.edu.sjtu.literature.entity.Literature;
import cn.edu.sjtu.literature.web.util.Constants;
import cn.edu.sjtu.literature.web.view.View;

public class InitLiteratureFormController extends AbstractController {

	@Override
	public View doAction(HttpServletRequest req, HttpServletResponse resp) {
		log.debug("do literature form");
		
		String id = req.getParameter("id");
		
		// add categories to view
		req.setAttribute(Constants.CATEGORIES, literatureService.getCategories());
		
		if (StringUtils.isNotBlank(id) && Integer.parseInt(id) > 0) {
			// get literature
			Literature l = literatureService.getLiterature(Integer.parseInt(id));
			req.setAttribute(Constants.LITERATURE, l);
		}
		
		return getView("literature/form.jsp");
	}

}
